<?php
/**
 * Created by PhpStorm.
 * User: 23783
 * Date: 2018/3/8
 * Time: 16:36
 */
namespace Common;

class CommonMethod{

    protected $db;
    public function __construct()
    {
        global $DB;
        $this->db = $DB;
    }


    /**
     * 审批流程字符串处理
     * @param $list
     * @return array
     */
    function StrApprovalProcess($list){
        $expList =explode('。',$list);
        $newAry = array();
        foreach ($expList as $value){
            if(!empty($value)){
                if(strstr($value,'[')){
                    $newAry[]=  substr($value,0,strpos($value, '[')).''.substr($value,strpos($value,']')+1);
                }else{
                    $newAry[] = $value.'。';
                }
            }
        }
        return $newAry;
    }

    /**
     * 获取工作流程模板ID
     * @param $document_id
     * @return mixed
     */
    function GetIblockById($document_id){
        if(!empty($document_id)){
            $sql ="SELECT b.`CODE`
                   FROM  `b_iblock_element` AS a
                   JOIN `b_iblock` AS b ON a.`IBLOCK_ID` =b.`ID`
                   WHERE a.`ID`=".$this->db->ForSql($document_id);
            $data = $this->db->Query($sql,true);
            $dataRow = $data->Fetch();
            return intval($dataRow['CODE']);
        }
    }
    /**
     * 部门ID取部门名称
     * @param $department_id
     * @return mixed
     */
    function DepartmentIdByName($department_id){
        if(!empty($department_id)){
            $department  =  unserialize($department_id);
            $block =\CIBlockSection::GetByID($department[0]);
            $depart = $block->Fetch();
            return $depart['NAME'];
        }
    }

    /**
     * 返回参数枚举类型
     * @param $property_enum_id
     * @return mixed
     */
    function PropertyEnumIdByName($property_enum_id){
        if(!empty($property_enum_id)){
            $sql ="SELECT * FROM `b_iblock_property_enum` WHERE `ID`=".$this->db->ForSql($property_enum_id);
            $data = $this->db->Query($sql,true);
            $dataRow = $data->Fetch();
            return $dataRow['VALUE'];
        }
    }
    /**
     * 数字转换为中文
     * @param $num
     * @return string
     */
    function NumToUpper($num) {
        static $cnums=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"),
        $cnyunits=array("","圆","角","分"),
        $grees=array("","拾","佰","仟","万","拾","佰","仟","亿");
        list($ns1,$ns2)=explode(".",$num,2);

        $ns2=array_filter(array($ns2[1],$ns2[0])); //转为数组

        $arrayTemp=$this->_cny_map_unit(str_split($ns1),$grees);

        $ret=array_merge($ns2,array(implode("",$arrayTemp),"")); //处理整数

        $arrayTemp=$this->_cny_map_unit($ret,$cnyunits);

        $ret=implode("",array_reverse($arrayTemp));     //处理小数

        return str_replace(array_keys($cnums),$cnums,$ret).'整';
    }

    function _cny_map_unit($list,$units) {
        $ul=count($units);
        $xs=array();
        foreach (array_reverse($list) as $x) {
            $l=count($xs);
            if ($x!="0" || !($l%4)) $n=($x=='0'?'':$x).($units[($l)%$ul]);
            else $n=is_numeric($xs[0][0])?$x:'';
            array_unshift($xs,$n);
        }
        return $xs;
    }
}